﻿using System;
using System.Collections.Generic;
using System.Text;
using umbraco.BusinessLogic.Actions;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;

namespace SS4U.Section.Configuration
{
    public class LoadConfiguration: BaseTree
    {
        private const string SplitString = "|";

        public LoadConfiguration(string application)
            : base(application) 
        { 
        }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.Text = "Configuration";
            rootNode.Icon = FolderIcon;
            rootNode.OpenIcon = FolderIconOpen;
            rootNode.NodeType = string.Format("init{0}", TreeAlias);
            rootNode.NodeID = "-1";

            rootNode.Menu.Clear();
            rootNode.Menu.Add(ActionNew.Instance);
        }

        public override void Render(ref XmlTree tree)
        {
            var configRoot = Library.Configuration.Root;

            // Root Node of the config tree
            if (NodeKey == string.Empty)
            {
                foreach (var definition in configRoot.SearchDefinitions)
                {
                    var xNode = XmlTreeNode.Create(this);
                    xNode.NodeID = definition.Id.ToString();
                    xNode.Text = definition.Name;
                    xNode.Icon = "doc.gif";
                    xNode.Action = string.Format("javascript:EditSearchDefinition('{0}');", definition.Id);

                    xNode.Menu.Clear();
                    xNode.Menu.AddRange(new List<IAction> { ActionNew.Instance, ActionDelete.Instance });

                    var treeService = new TreeService(-1, TreeAlias, ShowContextMenu, IsDialog, DialogMode, app, string.Format("SearchDefinition{0}{1}", SplitString, definition.Name));
                    xNode.Source = treeService.GetServiceUrl();

                    tree.Add(xNode);
                }
            }
            else
            {
                var keys = NodeKey.Split(SplitString.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                switch (keys[0])
                {
                    case "SearchDefinition":
                        var definition = configRoot.SearchDefinitions.GetByName(keys[1]);
                        foreach (var documentType in definition.DocumentTypes)
                        {
                            var xNode = XmlTreeNode.Create(this);
                            xNode.NodeID = documentType.Alias;
                            xNode.Text = documentType.Alias;
                            xNode.Icon = "doc.gif";
                            xNode.Action = string.Format("javascript:EditDocumentType('{0}', '{1}');", documentType.Alias, definition.Name);

                            xNode.Menu.Clear();
                            //xNode.Menu.AddRange(new List<IAction> { ActionNew.Instance, ActionDelete.Instance });

                            //var treeService = new TreeService(-1, TreeAlias, ShowContextMenu, IsDialog, DialogMode, app, string.Format("DocumentType{0}{1}", SplitString, documentType.Alias));
                            xNode.Source = string.Empty; // treeService.GetServiceUrl();

                            tree.Add(xNode);
                        }
                        break;
                    case "DocumentType":
                        break;
                }
            }
        }

        public override void RenderJS(ref StringBuilder javascript)
        {
            javascript.Append(
                @"
                    function EditSearchDefinition(id) {
	                    parent.right.document.location.href = 'plugins/SS4U/EditSearchDefinition.aspx?id=' + id;
                    }
                    function EditDocumentType(id, definition) {
	                    parent.right.document.location.href = 'plugins/SS4U/EditDocumentType.aspx?id=' + id + '&definition=' + definition;
                    }
                ");
        }

    }
}